package com.xiaoniu.forum.web.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoniu.forum.common.constant.ForumHttpStatusConstant;
import com.xiaoniu.forum.common.utils.PageUtils;
import com.xiaoniu.forum.common.utils.R;
import com.xiaoniu.forum.entity.CommunityTopicCommentEntity;
import com.xiaoniu.forum.entity.CommunityTopicEntity;
import com.xiaoniu.forum.entity.vo.*;
import com.xiaoniu.forum.form.AppCommentForm;
import com.xiaoniu.forum.form.AppPageForm;
import com.xiaoniu.forum.form.AppTopicForm;
import com.xiaoniu.forum.renren.common.annotation.SysLog;
import com.xiaoniu.forum.service.AppCommunityService;
import com.xiaoniu.forum.service.CommunityTopicCommentService;
import com.xiaoniu.forum.service.CommunityTopicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/app/community")
@Api("社区接口")
public class AppCommunityController {

    @Autowired
    private AppCommunityService appCommunityService;
    @Autowired
    private CommunityTopicCommentService commentService;
    @Autowired
    private CommunityTopicService topicService;


    @GetMapping("/index")
    @ApiOperation("查询社区首页数据列表")
    public R communityIndex(){
        List<AppCommunityIndexVo> list = appCommunityService.getIndexList();
        return R.ok().put("data",list);
    }

    @GetMapping("/community/{id}")
    @ApiOperation("根据板块id获取板块详情")
    public R getCommunityInfoById(@PathVariable String id){
        AppCommunityInfoVo communityInfo = appCommunityService.getCommunityInfoById(id);
        return R.ok().put("data",communityInfo);
    }

    @GetMapping("/topicCategory/{id}")
    @ApiOperation("根据板块id获取话题分类列表")
    public R getTopicCategoryById(@PathVariable String id){
        List<AppTopicCategoryVo> list = appCommunityService.getTopicCategoryById(id);
        return R.ok().put("data",list);
    }

    @GetMapping("/topicTop/{id}")
    @ApiOperation("根据板块ID获取话题置顶和公告列表")
    public R getTopicTopsById(@PathVariable(required = false) String id){
        List<AppCommunityTopTopicVo> list = appCommunityService.getTopicTopsById(id);
        return R.ok().put("data",list);
    }

    @PostMapping({"/topic/{communityId}/{topicCategoryId}","/topic/{communityId}"})
    @ApiOperation("根据话题分类ID获取话题列表")
    public R getTopicsById(@PathVariable(value = "communityId")  String communityId,
                           @PathVariable(name = "topicCategoryId",required = false) String topicCategoryId,
                           @RequestBody AppPageForm pageForm)
    {
        PageUtils page = appCommunityService.getTopicsById(communityId,topicCategoryId,pageForm);
        return R.ok().put("data",page);
    }

    @GetMapping("/topicInfo/{id}")
    @ApiOperation("根据话题id获取话题详情")
    public R getTopicInfoById(@PathVariable String id){
        AppCommunityTopicInfoVo appCommunityTopicInfoVo = appCommunityService.getTopicInfoById(id);
        return R.ok().put("data",appCommunityTopicInfoVo);
    }

    @PostMapping("/topicComments/{id}")
    @ApiOperation("根据话题id获取评论列表")
    public R getTopicCommentsById(@PathVariable String id,@RequestBody AppPageForm pageForm){
        PageUtils page = appCommunityService.getTopicCommentsById(id,pageForm);
        return R.ok().put("data",page);
    }


    /**
     * 根据id批量查询评论
     * 只查评论的id以及内容以及评论人id
     */
    @ApiOperation("根据id批量查询评论")
    @PostMapping("/comment/batch/info")
    public R batchQueryComment(@RequestBody List<String> ids){
        if (ids.size()==0){
            return R.ok();
        }
        QueryWrapper<CommunityTopicCommentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","reply_text","user_id");
        queryWrapper.in("id",ids);
        List<CommunityTopicCommentEntity> comments = commentService.list(queryWrapper);
        return R.ok().put("data",comments);
    }

    /**
     * 根据id批量查询主题
     */
    @ApiOperation("根据id批量查询话题")
    @PostMapping("/topic/batch/info")
    public R batchQueryTopic(@RequestBody List<String> ids){
        if (ids.size()==0){
            return R.ok();
        }
        QueryWrapper<CommunityTopicEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        List<CommunityTopicEntity> topics = topicService.list(queryWrapper);
        return R.ok().put("data",topics);
    }

    @ApiOperation("获取用户是否签到了社区")
    @GetMapping("/checkinStatus/{communityId}/{memberId}")
    public R getCheckinStatusByMemberId(@PathVariable String communityId,@PathVariable String memberId){
        boolean isCheckin = appCommunityService.isCheckin(communityId,memberId);
        if (isCheckin){
            return R.ok().put("code", ForumHttpStatusConstant.CHECKIN);
        }else {
            return R.ok().put("code", ForumHttpStatusConstant.NO_CHECKIN);
        }
    }

    @ApiOperation("用户社区签到")
    @PostMapping("/checkin/{communityId}/{memberId}")
    public R checkinByMemberId(@PathVariable String communityId,@PathVariable String memberId){
        appCommunityService.checkin(communityId,memberId);
        return R.ok();
    }

    @ApiOperation("评论")
    @PostMapping("/comment")
    public R commentByTopicIdAndMemberId(@RequestBody AppCommentForm commentForm){
        appCommunityService.saveComment(commentForm);
        return R.ok();
    }

    @ApiOperation("发布话题")
    @PostMapping("/topic")
    public R saveTopic(@RequestBody AppTopicForm topicForm){
        appCommunityService.saveTopic(topicForm);
        return R.ok();
    }

    /**
     * 根据用户id分页查询话题（根据创建时间倒序）
     */
    @ApiOperation("根据用户id查询话题")
    @PostMapping("/topic/listByUserId/{userId}")
    public R listTopicByUserId(@PathVariable String userId,@RequestBody Page page){
        PageUtils pageUtils = appCommunityService.listTopicByUserId(userId,page);
        return R.ok().put("page",pageUtils);
    }

    /**
     * 根据用户id分页查询该用户的评论（根据创建时间倒序）
     * 每个评论包含回复的评论
     */
    @ApiOperation("根据用户id查询话题")
    @PostMapping("/comment/listByUserId/{userId}")
    public R listCommentByUserId(@PathVariable String userId,@RequestBody Page page){
        System.out.println(page);
        PageUtils pageUtils = appCommunityService.listCommentByUserId(userId,page);
        return R.ok().put("page",pageUtils);
    }

    /**
     * 三级联查相关--------------------开始开始开始
     */

    @ApiOperation("查询所有社区分类(联级选择)")
    @GetMapping("/communityCategory/option")
    public R getCommunityCategoryForOption(){
        List<AppPublishOptionVo> list = appCommunityService.getCommunityCategoryForOption();
        return R.ok().put("data",list);
    }

    @ApiOperation("根据id查询社区名(联级选择)")
    @GetMapping("/community/option/{id}")
    public R getCommunityCategoryForOptionById(@PathVariable String id){
        List<AppPublishOptionVo> list = appCommunityService.getCommunityForOptionById(id);
        return R.ok().put("data",list);
    }

    @ApiOperation("根据id查询话题分类(联级选择)")
    @GetMapping("/topicCategory/option/{id}")
    public R getTopicCategoryForOptionById(@PathVariable String id){
        List<AppPublishOptionVo> list = appCommunityService.getTopicCategoryForOptionById(id);
        return R.ok().put("data",list);
    }

    /**
     * 三级联查相关--------------------结束结束结束
     */

    @ApiOperation("根据评论id查询评论详情")
    @GetMapping("/topic/commentInfo/{id}")
    public R getCommentInfoById(@PathVariable String id){
        AppCommunityCommentListVo topicInfo = appCommunityService.getCommentInfoById(id);
        return R.ok().put("data",topicInfo);
    }



}
